import { defineStore } from 'pinia'

export const useUserStore = defineStore({
  id: 'user',
  state: () => ({
    // 用户信息
    profile: JSON.parse(sessionStorage.getItem('user')) || {},
    // 登录后回跳路径
    redirectUrl: '/'
  }),
  getters: {
    token: (state) => state.profile.token || ''
  },
  actions: {
    // async insertPost (data) {
      // 可以做异步
      // await doAjaxRequest(data);
    //   this.name = data
    // }
    setUserInfo(val){
      sessionStorage.setItem('user', JSON.stringify(val))
      this.profile = val
    },
    delUserInfo(){
      return new Promise(resolve =>{
        this.profile = {}
        resolve()
      })
    }
  }
})
